package linkedList;
import java.util.Scanner;

    class Node {
        int num;
        Node next;
    }

    class LinkList {
        public Node Delete(Node head, int n) {
            Node p1 = new Node();
            Node p2 = new Node();
            if (head == null) {
                System.out.println("list null");
                return head;
            }
            p1 = head;
            while (n != p1.num && p1.next != null) {
                p2 = p1;
                p1 = p1.next;
            }
            if (p1.num == n) {
                if (p1 == head) head = p1.next;
                else p2.next = p1.next;

            } else
                System.out.println(n + " not fond");
            return head;
        }


        public Node add(Node head){
            int num;
            int i = 1;
            Node p = new Node();
            Node p1;
            Node p2 = new Node();
            Scanner in = new Scanner(System.in);
            System.out.print("Input the order:");
            num = in.nextInt();
            System.out.print("Input the add node:");
            p.num = in.nextInt();
            p1 = head;
            while(i != num && p1.next!=null)
            {
                p2 = p1;
                p1 = p1.next;
                i++;
            }
            if(num == i)
            {
                if(p1 == head)
                {
                    if(p1.next!=null)
                    {
                        p.next = p1;
                        head = p;
                    }
                    else
                    {
                        head = p;
                    }
                }
                else
                {
                    p.next = p2.next;
                    p2.next = p;
                }

            }
            else if(num == (i+1))
            {
                p1.next = p;
            }
            else
                System.out.println("the order is wrong!");
            return head ;
        }

        public Node creat(int n)
        {
            int i;
            Node head,pend;
            Node pnew = new Node();
            Scanner in = new Scanner(System.in);
            System.out.print("Input the node "+1+":");
            pnew.num = in.nextInt();
            head = pnew;
            pend = pnew;
            for(i = 2;i <=n;i++)
            {
                pnew = new Node();
                System.out.print("Input the node "+i+":");
                pnew.num = in.nextInt();
                pend.next = pnew;
                pend = pnew;

            }
            pend.next=null;
            return head;
        }
        public void print(Node head)
        {
            Node temp;
            temp = head;
            while(temp != null)
            {
                System.out.print(temp.num);
                temp = temp.next;
                if(temp != null)
                    System.out.print("-->");
            }
            System.out.println();
        }

    }

